<?php

class XistsUser extends BaseXistsUser
{
  private $_sibling = null;
    
  
  public function activate($status)
  {
    $this->setIsActivated($status);
    if ($status) {
      $this->setJoinDate(date('Y-m-d H:i:s'));
      $this->setPlanExpiresAt(self::calPlanExpiresDate());
    } else {
      $this->setJoinDate(null);
    }
    $this->save();
    return true;
  }
  
  public function save(PropelPDO $con = null)
  {
	  if ($this->isNew()) {
		  $this->setPasswd(md5($this->getPasswd()));
      if (!$this->getPlanExpiresAt()) {
        $this->setPlanExpiresAt(self::calPlanExpiresDate());
      }
		}
    return parent::save($con);
  }

  public function hasPositionEmpty($position)
  {
    $sibling = $this->getSibling();
    if (!isset($sibling[$position])) {
      return false;
    }
    if ($sibling[$position] == false) {
       return true;
    }
    return false;
  }
  
  public function getSibling($position = null)
  {
    if (!$this->_sibling) {
      $this->_sibling = XistsUserPeer::getSibling($this->getUsername());
    }
    if (!$position) {
      return $this->_sibling;
    }
    return $this->_sibling[$position];
  }
  
  public function getFollowers()
  {
    return XistsUserPeer::getFollowers($this->getUsername());
  }
  
  public function getPrevMemberCount($position)
  {
    $count = 0;
    $siblingUser = $this->getSibling($position);
    if ($siblingUser) {
      if ($siblingUser->getCreatedAt('U') < self::getFirstDay(true)) {
        $count++;
      }
      $count += XistsUserPeer::getMemberCount($siblingUser->getUsername(), self::getPrevMonthCriteria());
    }
    return $count;
  }
  
  static private function getPrevMonthCriteria() 
  {
    $criteria = new Criteria();
    $criteria->add(XistsUserPeer::CREATED_AT, self::getFirstDay(), Criteria::LESS_THAN);
    return $criteria;
  }
  
  public function getCurMemberCount($position)
  {
    $count = 0;
    $siblingUser = $this->getSibling($position);
    if ($siblingUser) {
      if ($siblingUser->getCreatedAt('U') >= self::getFirstDay(true)) {
        $count++;
      }
      $count += XistsUserPeer::getMemberCount($siblingUser->getUsername(), self::getCurMonthCriteria());
    }
    return $count;
  }
  
  static private function getCurMonthCriteria() 
  {
    $criteria = new Criteria();
    $criteria->add(XistsUserPeer::CREATED_AT, self::getFirstDay(), Criteria::GREATER_EQUAL);
    return $criteria;
  }
  
  public function getPrevAmountTotal($position)
  {
    $amount = 0;
    $siblingUser = $this->getSibling($position);
    if ($siblingUser) {
      if ($siblingUser->getCreatedAt('U') < self::getFirstDay(true)) {
        $amount = $siblingUser->getDepositAmount();
      }
      $amount += XistsUserPeer::getAmountCount($siblingUser->getUsername(), self::getPrevMonthCriteria());
    }
    return $amount;
  }
  
  public function getCurAmountTotal($position)
  {
    $amount = 0;
    $siblingUser = $this->getSibling($position);
    if ($siblingUser) {
      if ($siblingUser->getCreatedAt('U') >= self::getFirstDay(true)) {
        $amount = $siblingUser->getDepositAmount();
      }
      $amount += XistsUserPeer::getAmountCount($siblingUser->getUsername(), self::getCurMonthCriteria());
    }
    return $amount;
  }
  
  public function getMyReturns()
  {
    $criteria = $this->getUserCriteria();
    return XistsReturnsPeer::doSelect($criteria);
  }
  
  public function getReferelIncome()
  {
    $criteria = $this->getUserCriteria();
    return XistsReferalIncomePeer::doSelect($criteria);
  }
  
  public function getMatchingIncome()
  {
    $criteria = $this->getUserCriteria();
    return XistsMatchingIncomePeer::doSelect($criteria);
  }
  
  public function getTotalIncome()
  {
    $criteria = $this->getUserCriteria();
    return XistsTotalIncomePeer::doSelect($criteria);
  }
  
  private function getUserCriteria()
  {
    $criteria = new Criteria();
    $criteria->add(XistsReturnsPeer::USER_ID, $this->getId());
    return $criteria;
  }
  
  static private function getFirstDay($asTime = false)
  {
    $time = mktime(0, 0, 0, date('m'), 1, date('Y'));
    if ($asTime) {
      return $time;
    }
    return date("Y-m-d", $time);
  }
  
  static private function calPlanExpiresDate()
  {
    return strtotime("+".sfConfig::get('app_plan_expires_months')." months");
  }
  
  public function __toString()
  {
    return sprintf('%s (%s)', $this->getUsername(), $this->getEmail());
  }
  
}